'''
Contact View
'''

from django.http import HttpResponseRedirect
from django.shortcuts import render_to_response
from django.template import RequestContext
from django.contrib.auth.views import redirect_to_login

from contact import ContactForm

def contact_form(request, form_class=ContactForm, template_name='contact/contact.html', success_url='/contact/sent/', login_required=False, fail_silently=False):
    '''
    Renders a contact form and sends it through email
    form_class: specify ContactForm class to use
    template_name: specify the template to use for rendering
    success_url: specify url to redirect to after sending a message
    login_required: specify registered user requirement
    '''

    if login_required and not request.user.is_authenticated():
        return redirect_to_login(request.path)

    if request.method == 'POST':
        form = form_class(request.POST, request=request)
        if form.is_valid():
            form.send(fail_silently=fail_silently)
            return HttpResponseRedirect(success_url)
    else:
        form = form_class(request=request)
    return render_to_response(template_name,
                                { 'form': form },
                                context_instance=RequestContext(request))

def sent(request):
    return render_to_response('contact/sent.html', context_instance=RequestContext(request))
